
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from meiduo_mall import settings



def generic_openid_token(openid):

    # 1、创建序列化器
    serializer = Serializer(secret_key=settings.SECRET_KEY,expires_in=60*60)
    # 2、组织数据
    data = {'openid':openid}
    # 3、加密处理
    token = serializer.dumps(data)
    # 4、加密之后是bytes类型，转换为字符串
    return token.decode()


from itsdangerous import BadTimeSignature,BadSignature

def check_access_token(token):

    #1、创建序列化器
    serializer = Serializer(secret_key=settings.SECRET_KEY,expires_in=3600)

    #2、对数据进行解密
    try:
        data = serializer.loads(token)
    except BadSignature:
        return None

    openid = data.get('openid')

    return openid









